package Controller

import (
	"back_go/Common"
	"back_go/DAO"
	models "back_go/DAO/model"
	"back_go/Response"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

// Save @Summary 保存用户地址
// @Description 保存用户地址信息
// @ID save-user-address
// @Accept  json
// @Produce  json
// @Param TzUserAddr body models.TzUserAddr true "用户地址信息"
// @Success 200 {object} Response.ResponseBody "操作成功"
// @Failure 500 {object} Response.ResponseBody "服务器内部错误"
// @Router /user/addr [post]
func Save(c *gin.Context) {
	var engine = DAO.Engine
	var TzUserAddr models.TzUserAddr
	if err := c.BindJSON(&TzUserAddr); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "解析请求体失败", "detail": err.Error()})

		return
	}
	TzUserAddr.CreateTime = time.Now()
	_, err := engine.Insert(TzUserAddr)

	if err != nil {
		c.JSON(http.StatusInternalServerError, Response.ResponseBody{
			Code:      "A00005",
			Msg:       "服务器出了点小差",
			Data:      false,
			Version:   "1.0",
			Timestamp: time.Now().Unix(),
			Sign:      "",
			Success:   false,
			Fail:      true,
		})
		return
	}

	c.JSON(http.StatusOK, Response.ResponseBody{
		Code:      "00000",
		Msg:       "ok",
		Data:      true,
		Version:   "1.0",
		Timestamp: time.Now().Unix(),
		Sign:      "", // 根据你的业务逻辑生成签名
		Success:   true,
	})
}

// UpdateById @Summary 修改用户地址
// @Description 修改用户地址
// @ID update-user-address
// @Accept  json
// @Produce  json
// @Param TzUserAddr body models.TzUserAddr true "用户地址信息"
// @Success 200 {object} Response.ResponseBody "操作成功"
// @Failure 500 {object} Response.ResponseBody "服务器内部错误"
// @Router /user/addr [put]
func UpdateById(c *gin.Context) {
	var engine = DAO.Engine
	var TzUserAddr models.TzUserAddr
	_ = c.BindJSON(&TzUserAddr)
	TzUserAddr.UpdateTime = time.Now()
	_, err := engine.Where("user_id =?", TzUserAddr.UserId).Update(&TzUserAddr)
	if err != nil {
		fmt.Println(err)
		c.JSON(http.StatusInternalServerError, Response.ResponseBody{
			Code:      "A00005",
			Msg:       "服务器出了点小差",
			Data:      false,
			Version:   "1.0",
			Timestamp: time.Now().Unix(),
			Sign:      "",
			Success:   false,
			Fail:      true,
		})
		return
	}

	c.JSON(http.StatusOK, Response.ResponseBody{
		Code:      "00000",
		Msg:       "ok",
		Data:      true,
		Version:   "1.0",
		Timestamp: time.Now().Unix(),
		Sign:      "", // 根据你的业务逻辑生成签名
		Success:   true,
	})
}

// GetUserAddrPage @Summary 获取用户地址分页数据
// @Description 获取用户地址的分页数据
// @Accept json
// @Produce json
// @Param pageParam query Common.PageParam[models.TzUserAddr] false "分页参数"
// @Param tzUserAddr body models.TzUserAddr false "用户地址参数"
// @Success 200 {object} Response.ResponseBody
// @Failure 400 {object} Response.ResponseBody
// @Failure 500 {object} Response.ResponseBody
// @Router /user/addr/page [get]
func GetUserAddrPage(c *gin.Context) {
	var engine = DAO.Engine

	// 定义分页参数结构体
	var pageParam Common.PageParam[models.TzUserAddr]
	// 定义用户地址结构体
	var tzUserAddr models.TzUserAddr

	// 尝试绑定分页参数结构体
	_ = c.ShouldBind(&pageParam)
	if pageParam.Size == 0 {
		pageParam.Size = 10
	} else if pageParam.Current == 0 {
		pageParam.Current = 1
	}
	// 尝试绑定用户地址结构体
	_ = c.ShouldBind(&tzUserAddr)

	//总数目
	totalCount, err := engine.Count(new(models.TzUserAddr))

	var userAddrs []models.TzUserAddr
	err = engine.Limit(int(pageParam.Size), int(pageParam.Current)).Find(&userAddrs)
	if err != nil {
		fmt.Println(err)
		c.JSON(http.StatusInternalServerError, Response.ResponseBody{
			Code:      "A00005",
			Msg:       "服务器出了点小差",
			Data:      false,
			Version:   "1.0",
			Timestamp: time.Now().Unix(),
			Sign:      "",
			Success:   false,
			Fail:      true,
		})
		return
	}

	c.JSON(http.StatusOK, Response.ResponseBody{
		Code: "00000",
		Msg:  "ok",
		Data: Response.PageData{
			Size:    pageParam.Size,
			Records: userAddrs,
			Total:   totalCount,
			Current: pageParam.Current,
			Pages:   totalCount / pageParam.Size,
		},
		Version:   "1.0",
		Timestamp: time.Now().Unix(),
		Sign:      "", // 根据你的业务逻辑生成签名
		Success:   true,
	})
}

// GetById godoc
// @Summary Get user address by ID
// @Description Get a user address by its ID.
// @Tags User Address
// @Produce json
// @Param addrId path string true "Address ID"
// @Success 200 {object} Response.ResponseBody
// @Failure 400 {object} Response.ResponseBody
// @Router /user/addr/info/{addrId} [get]
func GetById(c *gin.Context) {
	var engine = DAO.Engine
	var TzUserAddr models.TzUserAddr
	addrId := c.Param("addrId")
	if addrId == "" {
		c.JSON(http.StatusBadRequest, Response.ResponseBody{
			Code:      "A00014",
			Msg:       "方法参数没有校验",
			Data:      nil,
			Version:   "1.0",
			Timestamp: time.Now().Unix(),
			Sign:      "", // 根据你的业务逻辑生成签名
			Success:   true,
		})
		return
	}
	_, _ = engine.Where("addr_id =?", addrId).Get(&TzUserAddr)

	c.JSON(http.StatusOK, Response.ResponseBody{
		Code:      "00000",
		Msg:       "ok",
		Data:      TzUserAddr,
		Version:   "1.0",
		Timestamp: time.Now().Unix(),
		Sign:      "",
		Success:   true,
		Fail:      false,
	})
}

// DeleteById   @Summary 删除用户地址
/*
  @Description 删除用户地址
  @Tags 用户地址
  @Accept json
  @Produce json
  @Param addrId path string true "用户地址ID"
  @Success 200 {object} Response.ResponseBody
  @Failure 400 {object} Response.ResponseBody
  @Router /user/addr/delete/{addrId} [delete] * @Description: 删除用户地址
*/
func DeleteById(c *gin.Context) {
	var engine = DAO.Engine
	addrId := c.Param("addrId")

	if addrId == "" {
		c.JSON(http.StatusBadRequest, Response.ResponseBody{
			Code:      "A00014",
			Msg:       "方法参数没有校验",
			Data:      nil,
			Version:   "1.0",
			Timestamp: time.Now().Unix(),
			Sign:      "",
			Success:   false,
		})
		return
	}

	var TzUserAddr models.TzUserAddr

	affected, err := engine.Where("addr_id =?", addrId).Delete(&TzUserAddr)
	if err != nil {

		c.JSON(http.StatusInternalServerError, Response.ResponseBody{
			Code:      "A00005",
			Msg:       "服务器出了点小差，无法执行删除操作",
			Data:      false,
			Version:   "1.0",
			Timestamp: time.Now().Unix(),
			Sign:      "",
			Success:   false,
		})

		return
	}
	if affected == 0 {
		c.JSON(http.StatusNotFound, Response.ResponseBody{
			Code:      "A00015",
			Msg:       "未找到要删除的用户地址记录",
			Data:      false,
			Version:   "1.0",
			Timestamp: time.Now().Unix(),
			Sign:      "",
			Success:   false,
		})
		return
	}

	c.JSON(http.StatusOK, Response.ResponseBody{
		Code:      "00000",
		Msg:       "User address deleted successfully",
		Data:      true,
		Version:   "1.0",
		Timestamp: time.Now().Unix(),
		Sign:      "",
		Success:   true,
	})
}
